﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System.Linq.Expressions;

namespace BS.Infra
{
    /// <summary>
    /// 仓储层
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseRepository<T>:IBaseRepository<T> where T:class,new()
    {
        private readonly BaseDbContext _dbContext;

        private readonly ILogger<BaseRepository<T>> _logger;
        public BaseRepository(BaseDbContext dbContext,ILogger<BaseRepository<T>> logger)
        {
            _dbContext = dbContext;
            _logger = logger;
        }
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<IList<T>> GetAllAsync()
        {
            try
            {
                return await _dbContext.Set<T>().ToListAsync();
            }
            catch (Exception e)
            {
                _logger.LogError("获取所有数据失败"+e.Message);
                throw;
            }
        }
        /// <summary>
        /// 获取查询数据
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task<IList<T>> GetAllAsync(Expression<Func<T, bool>> predicate)
        {
            try
            {
                return await _dbContext.Set<T>().Where(predicate).ToListAsync();
            }
            catch (Exception e)
            {
                _logger.LogError("获取查询数据失败" + e.Message);
                throw;
            }
        }
        /// <summary>
        /// 获取其中的一条数据
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task<T?> GetAsync(Expression<Func<T, bool>> predicate)
        {
            try
            {
                return await _dbContext.Set<T>().Where(predicate).FirstOrDefaultAsync();
            }
            catch (Exception e)
            {
                _logger.LogError("获取其中的一条数据失败" + e.Message);
                throw;
            }
        }
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> InsertAsync(T entity)
        {
            try
            {
                _dbContext.Set<T>().Add(entity);
                return await _dbContext.SaveChangesAsync();
            }
            catch (Exception e)
            {
                _logger.LogError("新增数据失败" + e.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T entity)
        {
            try
            {
                _dbContext.Set<T>().Update(entity);
                return await _dbContext.SaveChangesAsync();
            }
            catch (Exception e)
            {
                _logger.LogError("修改数据失败" + e.Message);
                throw;
            }
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public Task<int> InsertRangeAsync(IList<T> entities)
        {
            try
            {
                 _dbContext.Set<T>().AddRange(entities);
                return  _dbContext.SaveChangesAsync();
            }
            catch (Exception e)
            {
                _logger.LogError("新增数据失败" + e.Message);
                throw;
            }
        }

        /// <summary>
        /// 批量更新数据
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public  Task<int> UpdateRangeAsync(IList<T> entities)
        {
            try
            {
                _dbContext.Set<T>().UpdateRange(entities);
                return  _dbContext.SaveChangesAsync();
            }
            catch (Exception e)
            {
                _logger.LogError("更新失败" + e.Message);
                throw;
            }
        }

    }
}
